<!DOCTYPE html>
<!-- saved from url=(0041)http://c.biancheng.net/view/vip_7048.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" async="" charset="utf-8" src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/core.php"></script>

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
<!-- 启用Chromium高速渲染模式 -->
<meta name="renderer" content="webkit">
<meta name="force-rendering" content="webkit">
<!-- 禁止百度转码 -->
<meta name="applicable-device" content="pc,mobile">
<meta name="MobileOptimized" content="width">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<!-- 禁止识别电话号码 -->
<meta name="format-detection" content="telephone=no">

<link rel="shortcut icon" href="http://c.biancheng.net/favicon.ico?v=1.6.72">
<link href="./3.5Java项目实战：根据出生日期计算（判断）星座_files/common.css" rel="stylesheet">
<title>Java项目实战：根据出生日期计算（判断）星座</title>
<meta name="description" content="下面我们来了解一下十二星座对应的日期划分范围。 白羊：0321~0420 天秤：0924~1023金牛：0421~0521 天蝎：1024~1122双子：0522~0621 射手：1123~1221巨蟹：0622~0722 摩羯：1222~0120狮子：0723~0823 水瓶">
</head>
<body>
<div id="topbar" class="clearfix">
	<ul id="product-type" class="left">
		<li>
			<a href="http://c.biancheng.net/"><span class="iconfont iconfont-home"></span>首页</a>
		</li>
		<li class="active">
			<a href="http://c.biancheng.net/sitemap/" rel="nofollow"><span class="iconfont iconfont-book"></span>教程</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/vip/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-vip"></span>VIP会员</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/q2a/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-q2a"></span>一对一答疑</a>
		</li>
		<li>
			<a href="http://fudao.biancheng.net/" rel="nofollow" target="_blank"><span class="iconfont iconfont-fudao"></span>辅导班</a>
		</li>
	</ul>
</div>
<div id="header" class="clearfix">
	<a id="logo" class="left" href="http://c.biancheng.net/">
		<img height="26" src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/logo.png" alt="C语言中文网">
	</a>
	<ul id="nav-main" class="hover-none left clearfix">
		<li class="wap-yes"><a href="http://c.biancheng.net/">首页</a></li>
		<li><a href="http://c.biancheng.net/c/">C语言教程</a></li>
		<li><a href="http://c.biancheng.net/cplus/">C++教程</a></li>
		<li><a href="http://c.biancheng.net/python/">Python教程</a></li>
		<li><a href="http://c.biancheng.net/java/">Java教程</a></li>
		<li><a href="http://c.biancheng.net/linux_tutorial/">Linux入门</a></li>
		<li><a href="http://c.biancheng.net/sitemap/" title="网站地图">更多&gt;&gt;</a></li>
	</ul>
	<span id="sidebar-toggle" class="toggle-btn" toggle-target="#sidebar">目录 <span class="glyphicon"></span></span>

	<a href="http://vip.biancheng.net/?from=topbar" class="user-info glyphicon glyphicon-user hover-none" target="_blank" rel="nofollow" title="用户中心"></a>
</div>
<div id="main" class="clearfix">
	<div id="sidebar" class="toggle-target">
	<div id="contents">
		<dt><span class="glyphicon glyphicon-option-vertical" aria-hidden="true"></span><a href="http://c.biancheng.net/java/">Java教程</a></dt>
		
			<dd>
				<span class="channel-num">1</span>
				<a href="http://c.biancheng.net/java/10/">Java入门基础及环境搭建</a>
			</dd>
		
			<dd>
				<span class="channel-num">2</span>
				<a href="http://c.biancheng.net/java/20/">Java程序设计基础</a>
			</dd>
		<dd class="this"> <span class="channel-num">3</span> <a href="http://c.biancheng.net/java/30/">Java流程控制语句</a> </dd><dl class="dl-sub"><dd>3.1 <a href="http://c.biancheng.net/view/724.html">Java语句</a></dd><dd>3.2 <a href="http://c.biancheng.net/view/5737.html">Java if else语句</a></dd><dd>3.3 <a href="http://c.biancheng.net/view/738.html">Java switch case语句</a></dd><dd>3.4 <a href="http://c.biancheng.net/view/vip_7047.html">【Java项目实战】实现淡旺季飞机票打折</a><span class="glyphicon glyphicon-usd"></span></dd><dd class="active">3.5 <a href="http://c.biancheng.net/view/vip_7048.html">【Java项目实战】根据出生日期计算星座</a><span class="glyphicon glyphicon-usd"></span></dd><dd>3.6 <a href="http://c.biancheng.net/view/5742.html">Java while循环</a></dd><dd>3.7 <a href="http://c.biancheng.net/view/5743.html">Java for循环</a></dd><dd>3.8 <a href="http://c.biancheng.net/view/5746.html">Java for循环嵌套</a></dd><dd>3.9 <a href="http://c.biancheng.net/view/750.html">Java foreach语句</a></dd><dd>3.10 <a href="http://c.biancheng.net/view/751.html">Java return语句</a></dd><dd>3.11 <a href="http://c.biancheng.net/view/5755.html">Java break语句详解</a></dd><dd>3.12 <a href="http://c.biancheng.net/view/756.html">Java continue语句</a></dd><dd>3.13 <a href="http://c.biancheng.net/view/vip_7049.html">【Java项目实战】判断闰年平年并输出某月的天数</a><span class="glyphicon glyphicon-usd"></span></dd><dd>3.14 <a href="http://c.biancheng.net/view/760.html">Java输出杨辉三角形</a></dd><dd>3.15 <a href="http://c.biancheng.net/view/5769.html">Java选择结构和循环结构的总结</a></dd></dl>
			<dd>
				<span class="channel-num">4</span>
				<a href="http://c.biancheng.net/java/40/">Java字符串处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">5</span>
				<a href="http://c.biancheng.net/java/50/">Java数字和日期处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">6</span>
				<a href="http://c.biancheng.net/java/60/">Java内置包装类</a>
			</dd>
		
			<dd>
				<span class="channel-num">7</span>
				<a href="http://c.biancheng.net/java/70/">Java数组处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">8</span>
				<a href="http://c.biancheng.net/java/80/">Java类和对象</a>
			</dd>
		
			<dd>
				<span class="channel-num">9</span>
				<a href="http://c.biancheng.net/java/90/">Java继承和多态</a>
			</dd>
		
			<dd>
				<span class="channel-num">10</span>
				<a href="http://c.biancheng.net/java/100/">Java异常处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">11</span>
				<a href="http://c.biancheng.net/java/110/">Java集合、泛型和枚举</a>
			</dd>
		
			<dd>
				<span class="channel-num">12</span>
				<a href="http://c.biancheng.net/java/120/">Java反射机制</a>
			</dd>
		
			<dd>
				<span class="channel-num">13</span>
				<a href="http://c.biancheng.net/java/130/">Java输入/输出流</a>
			</dd>
		
			<dd>
				<span class="channel-num">14</span>
				<a href="http://c.biancheng.net/java/java/135/">Java注解</a>
			</dd>
		
	</div>
</div>
	<div id="article-wrap">
		<div id="article">
			<div class="arc-info">
	<span class="position"><span class="glyphicon glyphicon-map-marker"></span> <a href="http://c.biancheng.net/">首页</a> &gt; <a href="http://c.biancheng.net/java/">Java教程</a> &gt; <a href="http://c.biancheng.net/java/30/">Java流程控制语句</a></span>
	<span class="read-num">阅读：7,925</span>
</div>

<div id="ad-position-bottom"></div>
			<h1>Java项目实战：根据出生日期计算（判断）星座</h1>
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/vip_7047.html">【Java项目实战】实现淡旺季飞机票打折</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/5742.html">Java while循环</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-top"><p class="pic"></p><p class="text" adid="java-1v1q2a"><a href="http://c.biancheng.net/view/7552.html" target="_blank" style="color: #D33428;">C语言中文网推出辅导班啦，包括<span style="color: #008000;">「C语言辅导班、C++辅导班、算法/数据结构辅导班」</span>，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线，随时响应！</a></p></div>
			<div id="arc-body">下面我们来了解一下十二星座对应的日期划分范围。
<pre class="info-box">白羊：0321~0420          天秤：0924~1023
金牛：0421~0521          天蝎：1024~1122
双子：0522~0621          射手：1123~1221
巨蟹：0622~0722          摩羯：1222~0120
狮子：0723~0823          水瓶：0121~0219
处女：0824~0923          双鱼：0220~0320</pre>
例如，出生日期为 0609（6 月 9 号），则对应的是双子座。<br>
<br>
根据上述描述，在程序中需要用户输入一个 4 位数字，再根据这个数字所处的范围进行判断，其中前两位是月份，后两位是日期。这里我们分别用 if else 语句和 switch 语句实现，希望读者自己运行，以便更好的掌握&nbsp;if else 和 switch 的区别和适用场景。
<h2>
	switch实现计算星座功能</h2>
在这里使用 switch 语句判断出生的月份，然后根据日期确定星座名称。<br>
<br>
实现代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7048.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7048.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7048.html#">复制</a></pre></div><pre class="cpp sh_cpp snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span>String<span class="sh_symbol">[]</span> args<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>    System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"请输入您的出生年月（如 0123 表示 1 月 23 日）："</span><span class="sh_symbol">);</span></li><li>    <span class="sh_usertype">Scanner</span><span class="sh_normal"> </span>sc <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Scanner</span><span class="sh_symbol">(</span>System<span class="sh_symbol">.</span>in<span class="sh_symbol">);</span></li><li>    <span class="sh_type">int</span> monthday <span class="sh_symbol">=</span> sc<span class="sh_symbol">.</span><span class="sh_function">nextInt</span><span class="sh_symbol">();</span></li><li>    <span class="sh_type">int</span> month <span class="sh_symbol">=</span> monthday<span class="sh_symbol">/</span><span class="sh_number">100</span><span class="sh_symbol">;</span></li><li>    <span class="sh_type">int</span> day <span class="sh_symbol">=</span> monthday<span class="sh_symbol">%</span><span class="sh_number">100</span><span class="sh_symbol">;</span></li><li>    <span class="sh_usertype">String</span><span class="sh_normal"> </span>xingzuo <span class="sh_symbol">=</span> <span class="sh_string">""</span><span class="sh_symbol">;</span></li><li>    <span class="sh_keyword">switch</span> <span class="sh_symbol">(</span>month<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">1</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">21</span><span class="sh_symbol">?</span><span class="sh_string">"摩羯座"</span><span class="sh_symbol">:</span><span class="sh_string">"水瓶座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">2</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">20</span><span class="sh_symbol">?</span> <span class="sh_string">"水瓶座"</span><span class="sh_symbol">:</span><span class="sh_string">"双鱼座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">3</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">21</span><span class="sh_symbol">?</span><span class="sh_string">"双鱼座"</span><span class="sh_symbol">:</span><span class="sh_string">"白羊座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">4</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">21</span><span class="sh_symbol">?</span><span class="sh_string">"白羊座"</span><span class="sh_symbol">:</span><span class="sh_string">"金牛座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>        <span class="sh_keyword">case</span> <span class="sh_number">5</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">22</span><span class="sh_symbol">?</span><span class="sh_string">"金牛座"</span><span class="sh_symbol">:</span><span class="sh_string">"双子座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">6</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">22</span><span class="sh_symbol">?</span><span class="sh_string">"双子座"</span><span class="sh_symbol">:</span><span class="sh_string">"巨蟹座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">7</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">23</span><span class="sh_symbol">?</span><span class="sh_string">"巨蟹座"</span><span class="sh_symbol">:</span><span class="sh_string">"狮子座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">8</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">24</span><span class="sh_symbol">?</span><span class="sh_string">"狮子座"</span><span class="sh_symbol">:</span><span class="sh_string">"处女座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">9</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">24</span><span class="sh_symbol">?</span><span class="sh_string">"处女座"</span><span class="sh_symbol">:</span><span class="sh_string">"天秤座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">10</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">24</span><span class="sh_symbol">?</span><span class="sh_string">"天秤座"</span><span class="sh_symbol">:</span><span class="sh_string">"天蝎座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">11</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">23</span><span class="sh_symbol">?</span><span class="sh_string">"天蝎座"</span><span class="sh_symbol">:</span><span class="sh_string">"射手座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>　　    <span class="sh_keyword">case</span> <span class="sh_number">12</span><span class="sh_symbol">:</span></li><li>            xingzuo <span class="sh_symbol">=</span> day<span class="sh_symbol">&lt;</span><span class="sh_number">22</span><span class="sh_symbol">?</span><span class="sh_string">"射手座"</span><span class="sh_symbol">:</span><span class="sh_string">"摩羯座"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li>    <span class="sh_cbracket">}</span></li><li>    System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"您的星座是："</span> <span class="sh_symbol">+</span>xingzuo<span class="sh_symbol">);</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public static void main(String[] args) {
    System.out.println("请输入您的出生年月（如 0123 表示 1 月 23 日）：");
    Scanner sc = new Scanner(System.in);
    int monthday = sc.nextInt();
    int month = monthday/100;
    int day = monthday%100;
    String xingzuo = "";
    switch (month) {
　　    case 1:
            xingzuo = day&lt;21?"摩羯座":"水瓶座";
            break;
　　    case 2:
            xingzuo = day&lt;20? "水瓶座":"双鱼座";
            break;
　　    case 3:
            xingzuo = day&lt;21?"双鱼座":"白羊座";
            break;
　　    case 4:
            xingzuo = day&lt;21?"白羊座":"金牛座";
            break;
        case 5:
            xingzuo = day&lt;22?"金牛座":"双子座";
            break;
　　    case 6:
            xingzuo = day&lt;22?"双子座":"巨蟹座";
            break;
　　    case 7:
            xingzuo = day&lt;23?"巨蟹座":"狮子座";
            break;
　　    case 8:
            xingzuo = day&lt;24?"狮子座":"处女座";
            break;
　　    case 9:
            xingzuo = day&lt;24?"处女座":"天秤座";
            break;
　　    case 10:
            xingzuo = day&lt;24?"天秤座":"天蝎座";
            break;
　　    case 11:
            xingzuo = day&lt;23?"天蝎座":"射手座";
            break;
　　    case 12:
            xingzuo = day&lt;22?"射手座":"摩羯座";
            break;
    }
    System.out.println("您的星座是：" +xingzuo);
}</pre></div></div>
<br>
上述代码中， 首先声明变量 monthday 保存用户输入的信息，然后用 month 和 day 变量分别表示月份和日期。接下来 switch 语句根据月份判断执行 case 子句中的代码，在每个 case 子句中，使用三元运算符根据日期判断所属的星座。<br>
<br>
最终运行效果如下所示：
<pre class="info-box">请输入您的出生年月（如 0123 表示 1 月 23 日）：
0521
您的星座是：金牛座</pre>
<pre class="info-box">请输入您的出生年月（如 0123 表示 1 月 23 日）：
1109
您的星座是：天蝎座</pre>
<h2>
	if-else 实现计算星座功能</h2>
在这里我们使用嵌套 if，与 switch 一样首先判断出生的月份，然后根据日期确定星座名称。<br>
<br>
实现代码如下所示：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7048.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7048.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7048.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span>String<span class="sh_symbol">[]</span> args<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>    System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"请输入您的出生年月（如 0123 表示 1 月 23 日）："</span><span class="sh_symbol">);</span></li><li>    <span class="sh_usertype">Scanner</span><span class="sh_normal"> </span>sc <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Scanner</span><span class="sh_symbol">(</span>System<span class="sh_symbol">.</span>in<span class="sh_symbol">);</span></li><li>    <span class="sh_type">int</span> monthday <span class="sh_symbol">=</span> sc<span class="sh_symbol">.</span><span class="sh_function">nextInt</span><span class="sh_symbol">();</span></li><li>    <span class="sh_type">int</span> month <span class="sh_symbol">=</span> monthday <span class="sh_symbol">/</span> <span class="sh_number">100</span><span class="sh_symbol">;</span></li><li>    <span class="sh_type">int</span> day <span class="sh_symbol">=</span> monthday <span class="sh_symbol">%</span> <span class="sh_number">100</span><span class="sh_symbol">;</span></li><li>    <span class="sh_usertype">String</span><span class="sh_normal"> </span>xingzuo <span class="sh_symbol">=</span> <span class="sh_string">""</span><span class="sh_symbol">;</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">1</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">21</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"摩羯座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"水瓶座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">2</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">20</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"水瓶座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"双鱼座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">3</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">21</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"双鱼座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"白羊座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">4</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">21</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"白羊座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"金牛座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">5</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">22</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"金牛座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"双子座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">6</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">22</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"双子座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"巨蟹座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">7</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">23</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"巨蟹座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"狮子座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">8</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">24</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"狮子座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"处女座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">9</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">24</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"处女座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"天秤座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">10</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">24</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"天秤座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"天蝎座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">11</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">23</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"天蝎座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"射手座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>month <span class="sh_symbol">==</span> <span class="sh_number">12</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>day <span class="sh_symbol">&lt;</span> <span class="sh_number">22</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"射手座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>            xingzuo <span class="sh_symbol">=</span> <span class="sh_string">"摩羯座"</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li>    System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"您的星座是："</span> <span class="sh_symbol">+</span> xingzuo<span class="sh_symbol">);</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public static void main(String[] args) {
    System.out.println("请输入您的出生年月（如 0123 表示 1 月 23 日）：");
    Scanner sc = new Scanner(System.in);
    int monthday = sc.nextInt();
    int month = monthday / 100;
    int day = monthday % 100;
    String xingzuo = "";

    if (month == 1) {
        if (day &lt; 21) {
            xingzuo = "摩羯座";
        } else {
            xingzuo = "水瓶座";
        }
    } else if (month == 2) {
        if (day &lt; 20) {
            xingzuo = "水瓶座";
        } else {
            xingzuo = "双鱼座";
        }
    } else if (month == 3) {
        if (day &lt; 21) {
            xingzuo = "双鱼座";
        } else {
            xingzuo = "白羊座";
        }
    } else if (month == 4) {
        if (day &lt; 21) {
            xingzuo = "白羊座";
        } else {
            xingzuo = "金牛座";
        }
    } else if (month == 5) {
        if (day &lt; 22) {
            xingzuo = "金牛座";
        } else {
            xingzuo = "双子座";
        }
    } else if (month == 6) {
        if (day &lt; 22) {
            xingzuo = "双子座";
        } else {
            xingzuo = "巨蟹座";
        }
    } else if (month == 7) {
        if (day &lt; 23) {
            xingzuo = "巨蟹座";
        } else {
            xingzuo = "狮子座";
        }
    } else if (month == 8) {
        if (day &lt; 24) {
            xingzuo = "狮子座";
        } else {
            xingzuo = "处女座";
        }
    } else if (month == 9) {
        if (day &lt; 24) {
            xingzuo = "处女座";
        } else {
            xingzuo = "天秤座";
        }
    } else if (month == 10) {
        if (day &lt; 24) {
            xingzuo = "天秤座";
        } else {
            xingzuo = "天蝎座";
        }
    } else if (month == 11) {
        if (day &lt; 23) {
            xingzuo = "天蝎座";
        } else {
            xingzuo = "射手座";
        }
    } else if (month == 12) {
        if (day &lt; 22) {
            xingzuo = "射手座";
        } else {
            xingzuo = "摩羯座";
        }
    }
    System.out.println("您的星座是：" + xingzuo);
}</pre></div></div>
我们输入和上边一样的日期，运行结果如下：<br>
<pre class="info-box">请输入您的出生年月（如 0123 表示 1 月 23 日）：
0521
您的星座是：金牛座</pre>
<pre class="info-box">请输入您的出生年月（如 0123 表示 1 月 23 日）：
1109
您的星座是：天蝎座</pre>
</div>
			<div id="arc-append">
	<p>关注微信公众号「站长严长生」，在手机上阅读所有教程，随时随地都能学习。本公众号由<a class="col-link" href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">C语言中文网站长</a>运营，每日更新，坚持原创，敢说真话，凡事有态度。</p>
	<p style="margin-top:12px; text-align:center;">
		<img width="180" src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/qrcode_weixueyuan_original.png" alt="魏雪原二维码"><br>
		<span class="col-green">微信扫描二维码关注公众号</span>
	</p>
</div>
<!-- <p>关注微信公众号「魏雪原」，跟着<a class="col-link" href="/view/8092.html" target="_blank">站长</a>一起学习，拒绝躺平，跳出内卷，用知识完成阶级跃升：</p>
	<ul>
		<li>每日更新，坚持原创，敢说真话，凡事有态度。</li>
		<li>编程+求职+创业+营销，既有硬知识，也有软技能。</li>
		<li>手机端阅读教程，随时随地都能学习。</li>
		<li>即将绑定网站部分功能，包括注册登录、找回密码、获取下载链接等。</li>
	</ul> -->
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/vip_7047.html">【Java项目实战】实现淡旺季飞机票打折</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/5742.html">Java while循环</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-bottom"></div>

<!-- <div id="ad-bottom-weixin" class="clearfix">
	<div class="left" style="width: 535px;">
		<p><span class="col-red">编程帮</span>，一个分享编程知识的公众号。跟着<a class="col-link" href="/cpp/about/author/" target="_blank">站长</a>一起学习，每天都有进步。</p>
		<p>通俗易懂，深入浅出，一篇文章只讲一个知识点。</p>
		<p>文章不深奥，不需要钻研，在公交、在地铁、在厕所都可以阅读，随时随地涨姿势。</p>
		<p>文章不涉及代码，不烧脑细胞，人人都可以学习。</p>
		<p>当你决定关注「编程帮」，你已然超越了90%的程序员！</p>
	</div>
	<div class="right" style="width: 150px;">
		<img width="150" src="/templets/new/images/erweima_biancheng.gif?v=1.6.72" alt="编程帮二维码" /><br />
		<span class="col-green">微信扫描二维码关注</span>
	</div>
</div> -->

<div id="nice-arcs" class="box-bottom">
	<h4>优秀文章</h4>
	<ul class="clearfix">
<li><a href="http://c.biancheng.net/view/755.html" title="Linux chmod命令：修改文件或目录的权限">Linux chmod命令：修改文件或目录的权限</a></li>
<li><a href="http://c.biancheng.net/view/2614.html" title="Linux已经霸占了服务器领域">Linux已经霸占了服务器领域</a></li>
<li><a href="http://c.biancheng.net/view/2994.html" title="C# ThreadStart：创建线程">C# ThreadStart：创建线程</a></li>
<li><a href="http://c.biancheng.net/view/3644.html" title="Spark RDD是什么？">Spark RDD是什么？</a></li>
<li><a href="http://c.biancheng.net/view/4275.html" title="Spring使用AspectJ开发AOP：基于XML和基于Annotation">Spring使用AspectJ开发AOP：基于XML和基于Annotation</a></li>
<li><a href="http://c.biancheng.net/view/4888.html" title="JSP PageContext.removeAttribute()方法：移除指定范围对象中的属性">JSP PageContext.removeAttribute()方法：移除指定范围对象中的属性</a></li>
<li><a href="http://c.biancheng.net/view/5018.html" title="JSP JSTL &lt;sql:setDataSource&gt;标签：设置数据源">JSP JSTL &lt;sql:setDataSource&gt;标签：设置数据源</a></li>
<li><a href="http://c.biancheng.net/view/6463.html" title="暴力破解Telnet服务">暴力破解Telnet服务</a></li>
<li><a href="http://c.biancheng.net/view/vip_7324.html" title="Go语言使用事件系统实现事件的响应和处理">Go语言使用事件系统实现事件的响应和处理</a></li>
<li><a href="http://c.biancheng.net/view/7852.html" title="jQuery内容伪类选择">jQuery内容伪类选择</a></li>
</ul>
</div>
		</div>
		
	</div>
</div>
<script type="text/javascript">
// 当前文章ID
window.arcIdRaw = 'a_' + 7048;
window.arcId = "7c15btbTC0RdRtjb86Ck1plV0NDCKVv1MrVjrhANPUERHEZXPhKpl/OStac";
window.typeidChain = "93|79";
</script>
<div id="footer" class="clearfix">
	<div class="info left">
	<p>精美而实用的网站，分享优质编程教程，帮助有志青年。千锤百炼，只为大作；精益求精，处处斟酌；这种教程，看一眼就倾心。</p>
	<p>
		<a href="http://c.biancheng.net/view/8066.html" target="_blank" rel="nofollow">关于网站</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">关于站长</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8097.html" target="_blank" rel="nofollow">如何完成一部教程</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8093.html" target="_blank" rel="nofollow">联系我们</a> <span>|</span>
		<a href="http://c.biancheng.net/sitemap/" target="_blank" rel="nofollow">网站地图</a>
	</p>
	<p>Copyright ©2012-2021 biancheng.net, <a href="http://www.beian.miit.gov.cn/" target="_blank" rel="nofollow" style="color:#666;">陕ICP备15000209号</a></p>
	</div>
	<img class="right" src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/logo_bottom.gif" alt="底部Logo">
	<span id="return-top"><b>↑</b></span>
</div>

<script type="text/javascript">
window.siteId = 4;
window.cmsTemplets = "/templets/new";
window.cmsTempletsVer = "1.6.72";
</script>

<script src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/jquery1.12.4.min.js.下载"></script>
<script src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/common.js.下载"></script>
<span style="display:none;"><script src="./3.5Java项目实战：根据出生日期计算（判断）星座_files/z_stat.php" type="text/javascript" defer="defer" async="async"></script></span>

</body></html>